<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>restangular</title>
    <link rel="stylesheet" href="../lib/bootstrap3/css/bootstrap.min.css">
    <link rel="stylesheet" href="../lib/bootstrap3/css/dashboard.css">
    <script src="../lib/angularjs/angular.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.3.1/restangular.min.js"></script>
    <script src="http://cdn.bootcss.com/lodash.js/3.8.0/lodash.min.js"></script>
    <style>body{margin:10px 80px;}</style>
</head>
<body ng-app="app01">
<div class="panel panel-primary" ng-controller="ctrl01">
    <div class="panel-heading"><b>restangular：GET请求</b></div>
    <div class="panel-body">
        <p>
            <button class="btn btn-danger" ng-click="getList()">getList</button>&nbsp;&nbsp;<span class="bg-danger">var allUsers = User.getList(); // GET 【http://ip:port/ProjectName/users】</span>
        </p>
        <p class="well">
            {{message}}
        </p>
        <p>
            <button class="btn bg-danger" ng-click="findOne()">findOne</button>
        </p>
        <p class="well">
            {{message2}}
        </p>
        <p class="well">
            {{message2a}}
        </p>
        <p class="well">
            {{message2b}}
        </p>
        <p>
            <button class="btn bg-danger" ng-click="allUrl()">allUrl</button>
        </p>
        <p class="well">
            {{message3}}
        </p>
        <p>
            <button class="btn bg-danger" ng-click="singleSearch()">singleSearch</button>
        </p>
        <p class="well">
            {{message4}}
        </p>
    </div>
    <div class="panel-footer">

    </div>
</div>
<div class="panel panel-primary" ng-controller="ctrl02">
    <div class="panel-heading"><b>restangular：post请求</b></div>
    <div class="panel-body">
        <button class="btn bg-danger" ng-click="postFn()">postFn</button>
        {{message}}
    </div>
    <div class="panel-footer">
        author.get(); // GET/authors/abc123<br/>
        author.getList('books');// GET/authors/abc123/books<br/>
        author.put(); // PUT/authors/abc123<br/>
        author.post(); // POST/authors/abc123<br/>
        author.remove(); // DELETE/authors/abc123<br/>
        author.head(); // HEAD/authors/abc123<br/>
        author.trace(); // TRACE/authors/abc123<br/>
        author.options(); // OPTIONS/authors/abc123<br/>
        author.patch(); // PATCH/author/abc123<br/>
    </div>
</div>
<div class="panel panel-primary">
    <div class="panel-heading"><b></b></div>
    <div class="panel-body">

    </div>
    <div class="panel-footer">

    </div>
</div>

</body>
<script>
    var app=angular.module('app01',['restangular']);
    app.factory('UserService', ['Restangular', function(Restangular) {
        // 现在我们已经在UserService中访问了Restangular
        return Restangular.all('7.3.json');/** 这样设置Restangular服务会让所有的HTTP请求将/users路径作为根路径来拉取数据。**/
    }]);
</script>
<script>
    app.controller('ctrl01',function ($scope,UserService,Restangular) {
        $scope.getList=function () {
            $scope.message=UserService.getList();
        }
        $scope.findOne=function () {
            var oneUser = Restangular.one('users', 'abc123');/** 上面代码的效果是调用oneUser上的get()时向/users/abc123发送请求。 **/
            $scope.message2=oneUser;
            oneUser.get().then(function(user) {
                $scope.message2a=oneUser;
                // GET /users/abc123/inboxes
                $scope.message2b=user.getList('inboxes');
            });
        }
        $scope.allUrl=function () {
            // 搜索的所有URL都将使用
            // `http://google.com/`asthebaseUrl
            var searches = Restangular.allUrl('one', '7.3.json');
            // 将发送一个请求到GET http://google.com/
            $scope.message3=searches.getList();
        }
        $scope.singleSearch=function () {
//            var singleSearch = Restangular.oneUrl('betaSearch', 'http://beta.google.com/1');
            // 触发一个请求到GET http://google.com/1
            var singleSearch = Restangular.oneUrl('betaSearch', '7.3.json');
            $scope.message4=singleSearch.get();
        }
    });
</script>
<script>
    app.controller('ctrl02',function ($scope,Restangular) {
        var messages = Restangular.all('angularDemo/modules/7.3.json');
        $scope.postFn=function () {
            var newMessage = {body: 'Hello world'};
            $scope.message=messages.post(newMessage);
        }
     });
</script>
</html>